;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?orderUuid=") != -1) {
			var reqPath = basePath+'order/queryOrderById'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var order = res.data;
						$("#uuid").val(order.orderUuid);
						$("#name").val(order.name);
						$("#phone").val(order.phone);
						$("#province").val(order.province);
						$("#city").val(order.city);
						$("#county").val(order.county);
						$("#village").val(order.village);
						$("#address").val(order.address);
						$("#requirement").val(order.requirement);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else
		{
			//初始化列表
			initOrderData(1,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/udwin/order/addOrder.jsp';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initOrderData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveOrder();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateOrder();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editOrder(orderUuid)
{
	window.location.href = jumpPath + 'view/udwin/order/editOrder.jsp?orderUuid='+orderUuid;
}

function addOrderHandle(orderUuid)
{
	var url = jumpPath + 'view/udwin/order/listOrderHandle.jsp?orderUuid='+orderUuid;
	window.location.href = url;
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'order/deleteOrder?orderUuid='+id;
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == 'success')
			{
				initOrderData(1,15);
			}
		});
	}
}

function initOrderData(pageNumber,pagesize)
{
	var reqPath = basePath+'order/queryPage';
	var sendBody = '{"curPage": 1,"pageSize": 15';
	var $tab = $('#orderTables tbody').html("");

	
	var reqPath = basePath + 'order/queryPage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize+'';
	var $tab = $('#orderTables tbody').html("");
	
	var requestData = $('#frm_order').serializeObject();
	requestData = JSON.stringify(requestData);
	sendBody = sendBody+requestData.replace('{',',');

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var address = "";
					if(n.province!=null&&n.province!='null')
					{
						address += n.province;
					}
					if(n.city!=null&&n.city!='null')
					{
						address += n.city;
					}
					if(n.county!=null&&n.county!='null')
					{
						address += n.county;
					}
					if(n.village!=null&&n.village!='null')
					{
						address += n.village;
					}
					if(n.address!=null&&n.address!='null')
					{
						address += n.address;
					}
					var operation="<button onclick='editOrder(\""+n.orderUuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
								  "<button onclick='del(\""+n.orderUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					var name = "<button onclick='addOrderHandle(\""+n.orderUuid+"\")' type='button' class='btn btn-primary'>"+n.name+"</button>";
					rows += tpl_tr.replace(/\$orderUuid/g, n.orderUuid)
							      .replace(/\$name/,name)
							      .replace(/\$phone/, n.phone)
							      .replace(/\$address/, address)
							      .replace(/\$requirement/, n.requirement)
							      .replace(/\$createTime/, getLocalTime(n.createTime.toString().substr(0,10)))
							      .replace(/\$handleName/,n.handleName)
							      .replace(/\$userName/, n.userName)
							      .replace(/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(initOrderData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLoginOrderr
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveOrder()
{
	
	var reqOrdertPath = basePath + "order/addOrder";
	var requestData = $('#frm_order').serializeObject();
	requestData = JSON.stringify(requestData);
	send(reqOrdertPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/order/listOrder.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}


function updateOrder()
{
	
	var reqOrdertPath = basePath + "order/updateOrder";
	var requestData = $('#frm_order').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(reqOrdertPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/order/listOrder.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}
